package com.zyf.project.capchain.handler;

import com.zyf.project.capchain.exceptions.*;
import com.zyf.project.capchain.listener.result.template.ExceptionResultTemplate;
import com.zyf.project.capchain.listener.result.impl.Result;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * <h1>全局异常控制</h1>
 *
 * @author Zhou Yifan
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 空参数异常拦截
     *
     * @param e EmptyParamsException
     * @return Result
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = EmptyParamsException.class)
    public Result<EmptyParamsException> handlerEmptyParamsException(EmptyParamsException e) {
        ExceptionResultTemplate<EmptyParamsException> result = new ExceptionResultTemplate<>();
        return result.emptyParamsResult(e);
    }

    /**
     * 数据层异常拦截
     *
     * @param e SqlErrorException
     * @return Result
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = SqlErrorException.class)
    public Result<SqlErrorException> handlerSqlErrorException(SqlErrorException e) {
        ExceptionResultTemplate<SqlErrorException> result = new ExceptionResultTemplate<>();
        return result.sqlErrorResult(e);
    }

    /**
     * 服务层用户不存在异常拦截
     *
     * @param e SqlErrorException
     * @return Result
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = ServiceUserNotExistException.class)
    public Result<ServiceUserNotExistException> handlerServiceUserNotExistException(ServiceUserNotExistException e) {
        ExceptionResultTemplate<ServiceUserNotExistException> result = new ExceptionResultTemplate<>();
        return result.serviceUserNotExistResult(e);
    }

    /**
     * 服务层用户不存在异常拦截
     *
     * @param e SqlErrorException
     * @return Result
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = OpenidException.class)
    public Result<OpenidException> handlerOpenidException(OpenidException e) {
        ExceptionResultTemplate<OpenidException> result = new ExceptionResultTemplate<>();
        return result.wxLoginNoOpenidResult(e);
    }

    /**
     * 服务层用户不存在异常拦截
     *
     * @param e SqlErrorException
     * @return Result
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = WxUserNotExistException.class)
    public Result<WxUserNotExistException> handlerWxUserNotExistException(WxUserNotExistException e) {
        ExceptionResultTemplate<WxUserNotExistException> result = new ExceptionResultTemplate<>();
        return result.wxLoginUserNotExistResult(e);
    }


    /**
     * token异常拦截
     *
     * @param e TokenException
     * @return Result
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = TokenException.class)
    public Result<TokenException> handlerTokenException(TokenException e) {
        ExceptionResultTemplate<TokenException> result = new ExceptionResultTemplate<>();
        return result.wxTokenErrorException(e);
    }



}
